//
//  RadiateChartViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/9/12.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "RadiateChartViewController.h"
#import "RadiateChartView.h"
#import "GetRadiationRecordListApi.h"

@interface RadiateChartViewController ()

@property(nonatomic,strong) RadiateChartView *containerView;
@property(nonatomic,assign) NSInteger weekIndex;
@property(nonatomic,assign) NSInteger monthIndex;

@end

@implementation RadiateChartViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

- (void)refreshBaseControlValue
{
//    [[BlueToothDataHandle sharedInstance] getCurrentWeekWithIndex:0 result:^(NSString *startTime, NSString *endTime) {
//        NSLog(@"当前周：%@和%@",startTime,endTime);
//    }];
//    
//    [[BlueToothDataHandle sharedInstance] getCurrentMonthWithIndex:0 result:^(NSString *startTime, NSString *endTime) {
//        NSLog(@"当前月：%@和%@",startTime,endTime);
//    }];
    [self didClickRightButton];
}

#pragma mark - 点击事件
- (void)onRightButtonClick:(id)sender
{
    UIImage *screenShot = [OTScreenshotHelper screenshot];
    [ShareView showShareViewWithClickShareBlock:^(ShareType shareType) {
        [DTCSharePlatformManager shareWithImage:screenShot Type:shareType];
    }];
}

-(void)segmentAction:(UISegmentedControl *)Seg{
    
    NSInteger Index = Seg.selectedSegmentIndex;
    NSLog(@"Index %li", (long)Index);
    
    switch (Index) {
            
        case 0:
        {
            [self getRadiatonRecordListWithArray:[[BlueToothDataHandle sharedInstance]getWeekWithIndex:self.weekIndex]];
        }
            break;
        case 1:
        {
           [self getRadiatonRecordListWithArray:[[BlueToothDataHandle sharedInstance]getMonthWithIndex:self.monthIndex]];
        }
            break;
 
        default:
            
            break;
            
    }
    
}

- (void)didClickLeftButton
{
    switch (self.containerView.segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            self.weekIndex -= 1;
            if (self.weekIndex > 0)
            {
                self.weekIndex = 0;
            }
            
           [self getRadiatonRecordListWithArray:[[BlueToothDataHandle sharedInstance]getWeekWithIndex:self.weekIndex]];
        }
            break;
        case 1:
        {
            self.monthIndex -= 1;
            if (self.monthIndex > 0)
            {
                self.monthIndex = 0;
            }
            [self getRadiatonRecordListWithArray:[[BlueToothDataHandle sharedInstance]getMonthWithIndex:self.monthIndex]];

        }
            
            break;
            
        default:
            break;
    }
}

- (void)didClickRightButton
{
    switch (self.containerView.segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            self.weekIndex += 1;
            if (self.weekIndex > 0)
            {
                self.weekIndex = 0;
            }
           [self getRadiatonRecordListWithArray:[[BlueToothDataHandle sharedInstance]getWeekWithIndex:self.weekIndex]];
            
        }
            break;
        case 1:
        {
            self.monthIndex += 1;
            if (self.monthIndex > 0)
            {
                self.monthIndex = 0;
            }
            [self getRadiatonRecordListWithArray:[[BlueToothDataHandle sharedInstance]getMonthWithIndex:self.monthIndex]];

            
        }
            
            break;
            
        default:
            break;
    }
}

#pragma mark - 事件处理
- (void)getRadiatonRecordListWithArray:(NSArray *)dataArray
{
    NSString *startTime = dataArray.firstObject;
    NSString *endTime = dataArray.lastObject;
    NSString *dateString = [NSString stringWithFormat:@"%@.%@-%@.%@",startTime.subString(4,6),startTime.fromIndex(6),endTime.subString(4,6),endTime.fromIndex(6)];
    self.containerView.dateLabel.text = dateString;
    [SVProgressHUD showWithStatus:MyNSLocalizedString(@"正在加载中...")];
    GetRadiationRecordListApi *api = [[GetRadiationRecordListApi alloc] initWithDic:@{@"device_id":EXRACT_OBJECT(DEVICEID),@"sdate":startTime,@"edate":endTime}];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = [request responseJSONObject];
        if ([dict[@"errcode"] intValue] == 0)
        {
            NSMutableArray *array = [NSMutableArray array];
            CGFloat max = 0.0;
            CGFloat count = 0.0;
            NSArray *data = dict[@"data"];
            
            for (int i = 0; i < data.count; i++)
            {
                CGFloat radiate = [data[i][@"max_radiation"] floatValue];
                count += radiate;
                if (max < radiate)
                {
                    max = radiate;
                }
            }
            
            for (int j = 0; j < dataArray.count; j++)
            {
                NSString *dateStr = dataArray[j];
                BOOL has = NO;
                for (int i = 0; i < data.count; i++)
                {
                    CGFloat radiate = [data[i][@"max_radiation"] floatValue];
                    if ([data[i][@"date"] isEqualToString:dateStr])
                    {
                        has = YES;
                        [array addObject:[NSString stringWithFormat:@"%.2f",radiate]];
                        break;
                    }
                    else
                    {
                        has = NO;
                    }
                }
                
                if (!has)
                {
                    [array addObject:@"0"];
                }
            }
            if (array.count > 0)
            {
                [array insertObject:@"" atIndex:0];
            }
            self.containerView.radiateLabel.text = [NSString stringWithFormat:@"%.2fμT",max];
            if (array.count > 8)
            {
                self.containerView.unitLabel.text = MyNSLocalizedString(@"月辐射峰值");
            }
            else
            {
                self.containerView.unitLabel.text = MyNSLocalizedString(@"周辐射峰值");
            }
            if (data.count == 0)
            {
                [self.containerView setData:@[]];
            }
            else
            {
                [self.containerView setData:array];
            }
        }
        [SVProgressHUD dismiss];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:[request responseJSONObject][@"errmsg"]];
    }];
    
//    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request)
//    {
//       
//    } failure:nil];
}
//- (NSDate *)getDateWithIndex:(NSInteger)index
//{
//    NSDate *today = [[NSDate alloc] init];
//    self.dateIndex += 1;
//    if (self.dateIndex < 0)
//    {
//        NSTimeInterval secondsPerDay = 24 * 60 * 60 * self.dateIndex;
//        return [today dateByAddingTimeInterval: secondsPerDay];
//    }
//    else
//    {
//        self.dateIndex = 0;
//        return today;
//    }

//}

#pragma mark - 代理


#pragma mark - 通知处理

#pragma mark - 初始化
- (RadiateChartView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[RadiateChartView alloc]init];
        [superView addSubview:_containerView];
        [_containerView.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        [_containerView.leftButton addTarget:self action:@selector(didClickLeftButton) forControlEvents:UIControlEventTouchUpInside];
        [_containerView.rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];

    }
    return _containerView;
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:MyNSLocalizedString(@"辐射统计1")];
    [self setRightButtonImage:imageWithName(@"nav_icon_share")];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.right.bottom.equalTo(superView);
    }];
}
@end
